// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: ding <863086441@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
  // 获取项目&产品下子类列表
  async getlistsAction() {
    const departmentid = this.userinfo.departmentid;
    const data = await this.model('or_subject_childclass').where({ departmentid: departmentid }).select();
    return this.json(data);
  }

  // 项目&产品下子类新增、编辑
  async addeditAction() {
    const childclass = this.post();
    const id = childclass.id * 1;
    const orid = this.userinfo.departmentid;
    if (id) {
      // 编辑
      const updateInfo = {
        name: childclass.name,
        type: childclass.type,
        remarks: childclass.remarks
      };
      const res = await this.model('or_subject_childclass').where({ id: id }).update(updateInfo);
      return res ? this.success('更新成功!') : this.fail('更新失败!');
    } else {
      // 新增
      const addInfo = {
        departmentid: orid,
        name: childclass.name,
        type: childclass.type,
        remarks: childclass.remarks
      };
      const res = await this.model('or_subject_childclass').add(addInfo);
      return res ? this.success('添加成功!') : this.fail('添加失败!');
    }
  }

  // 删除项目&产品下子类
  async deleteoneAction() {
    const id = this.post().id;
    const res = await this.model('or_subject_childclass').where({ id: id }).delete();
    return res ? this.success('删除成功!') : this.fail('删除失败!');
  }
}
